package com.shangye.http;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

public class HttpServletRequest {
    private Socket socket;

    private String method;
    private String rui;
    private String protocol;
    private Map<String,String> headers = new HashMap<>();

    private String requestRUI;
    private String queryString;
    private Map<String,String> parameters = new HashMap<>();



    public HttpServletRequest(Socket socket) throws IOException ,EmptyRequestException{
        this.socket = socket;

        parseRequestLine();

        parseHeaders();

        parseContext();

    }

    private void parseRequestLine() throws IOException ,EmptyRequestException {

        String line = readLine();
        if (line.isEmpty()){
            throw new EmptyRequestException();
        }
        System.out.println("请求行："+line.toString().trim());
        String[] data = line.split("\\s");
        method = data[0];
        rui = data[1];
        protocol = data[2];
        parseRUI();
    }
    private void parseHeaders() throws IOException {
        while (true){
            String line = readLine();
            if (line.isEmpty()){
                break;
            }
            String[] data = line.split(":\\s");
            headers.put(data[0],data[1]);
//            System.out.println("消息头：" + line);
        }
        System.out.println(headers);
    }

    public void parseRUI(){
//        /writeArticle?title=sda&author=sad&content=

        String[] data = rui.split("\\?");
        requestRUI = data[0];
        if (data.length>1){
            queryString = data[1];
            try {
                queryString = URLDecoder.decode(queryString,"UTF-8");
                data = queryString.split("&");
                for (String para : data){
                    String[] split = para.split("=");
                    parameters.put(split[0],split[1]);
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }

    private void parseContext() {
    }

    private String readLine() throws IOException {
        InputStream inputStream = socket.getInputStream();
        int d;
        char pre = 'a',cur = 'a';
        StringBuilder builder = new StringBuilder();
        while (( d = inputStream.read())!=-1){
            cur = (char)d;
            if(pre == 13 && cur ==10){
                break;
            }
            builder.append(cur);
            pre = cur;
        }
       return builder.toString().trim();
    }

    public String getMethod() {
        return method;
    }

    public String getRui() {
        return rui;
    }

    public String getProtocol() {
        return protocol;
    }

    public String getHeaders(String key) {
        return headers.get(key);
    }

    public String getRequestRUI() {
        return requestRUI;
    }

    public String getQueryString() {
        return queryString;
    }

    public String getParameters(String name) {
        return parameters.get(name);
    }
}
